#include <core/StyleMgr.h>
#include <widgets/SImageButton.h>

#include <QHBoxLayout>
#include <QMouseEvent>
#include <QStylePainter>

#include "SDialog.h"

const char SDialogTitle::kbtnSettingObjName[16] = "sdlg_setting";
const char SDialogTitle::kName[16] = "SDialogTitle";

SDialogTitle::SDialogTitle(QWidget* parent) : QWidget(parent) {
  setObjectName(kName);
  m_pParent = qobject_cast<SDialog*>(parent);
  setMouseTracking(true);
  setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
  setFixedHeight(24);

  QHBoxLayout* titleLayout = new QHBoxLayout();
  titleLayout->setContentsMargins(0, 0, 0, 0);
  titleLayout->setSpacing(0);

  m_btnTopping = new SImageButton(this, "widgets_win_top");
  m_btnTopping->hide();
  m_btnFull = new SImageButton(this, "widgets_win_full");
  m_btnFull->hide();
  m_btnRestore = new SImageButton(this, "widgets_win_restore");
  m_btnRestore->hide();
  m_btnClose = new SImageButton(this, "widgets_win_close");
  m_btnClose->hide();
  m_btnPopup = new SImageButton(this, "widgets_win_out");
  m_btnPopup->hide();
  m_btnDisplay = new SImageButton(this, "widgets_double_angle_bracket_up");
  m_btnDisplay->setToolTip(tr("显示"));
  m_btnDisplay->hide();
  m_btnHide = new SImageButton(this, "widgets_double_angle_bracket_down");
  m_btnHide->setToolTip(tr("隐藏"));
  m_btnHide->hide();
  m_btnSetting = new SImageButton(this, "widgets_setting", kbtnSettingObjName);
  m_btnSetting->hide();

  titleLayout->addWidget(m_btnTopping);
  titleLayout->addStretch();
  titleLayout->addWidget(m_btnSetting);
  titleLayout->addWidget(m_btnFull);
  titleLayout->addWidget(m_btnRestore);
  titleLayout->addWidget(m_btnClose);
  titleLayout->addWidget(m_btnPopup);
  titleLayout->addWidget(m_btnDisplay);
  titleLayout->addWidget(m_btnHide);

  setLayout(titleLayout);

  connect(m_btnClose, &SImageButton::clicked, this,
          &SDialogTitle::onButtonClick);
  connect(m_btnFull, &SImageButton::clicked, this,
          &SDialogTitle::onButtonClick);
  connect(m_btnRestore, &SImageButton::clicked, this,
          &SDialogTitle::onButtonClick);
  connect(m_btnPopup, &SImageButton::clicked, this,
          &SDialogTitle::onButtonClick);
  connect(m_btnTopping, &SImageButton::clicked, this,
          &SDialogTitle::onButtonClick);
  connect(m_btnDisplay, &SImageButton::clicked, this,
          &SDialogTitle::onButtonClick);
  connect(m_btnHide, &SImageButton::clicked, this,
          &SDialogTitle::onButtonClick);
  connect(m_btnSetting, &SImageButton::clicked, this,
          &SDialogTitle::onButtonClick);
}

void SDialogTitle::setTitle(QString& strTitle) { m_strTitle = strTitle; }

void SDialogTitle::setChildWindow() {
  if (m_btnDisplay) m_btnDisplay->hide();
  if (m_btnHide) m_btnHide->hide();
  if (m_btnTopping) m_btnTopping->hide();
  if (m_btnFull) m_btnFull->hide();
  if (m_btnRestore) m_btnRestore->hide();
  if (m_btnClose) m_btnClose->hide();
  if (m_btnSetting && m_enableSetting) {
    m_btnSetting->show();
  }
  if (m_btnPopup && m_enablePopup) {
    m_btnPopup->show();
  }
}

void SDialogTitle::setPopupWindow() {
  if (m_btnTopping && m_enableTopping) {
    m_btnTopping->show();
  }
  if (isMaximized()) {
    if (m_btnFull) m_btnFull->hide();
    if (m_btnRestore) m_btnRestore->show();
  } else {
    if (m_btnFull) m_btnFull->show();
    if (m_btnRestore) m_btnRestore->hide();
  }
  if (m_btnClose) m_btnClose->show();
  if (m_btnPopup) m_btnPopup->hide();
}

void SDialogTitle::setEnablePopup(bool enabled) { m_enablePopup = enabled; }

void SDialogTitle::setEnableTopping(bool enabled) { m_enableTopping = enabled; }

void SDialogTitle::setEnableSetting(bool enabled) {
  m_enableSetting = enabled;

  if (m_enableSetting && m_btnSetting) {
    m_btnSetting->show();
  } else {
    m_btnSetting->hide();
  }
}

void SDialogTitle::setEnableDisplay(bool enabled) {
  m_enableDisplay = enabled;
  if (m_enableDisplay) {
    m_btnDisplay->show();
    m_btnHide->hide();
    m_pParent->setMinimumHeight(29);
  } else {
    m_btnHide->show();
    m_btnDisplay->hide();
    m_pParent->setMinimumHeight(314);
  }
}

void SDialogTitle::paintEvent(QPaintEvent* event) {
  if (m_strTitle.isEmpty()) return;

  QStylePainter painter(this);
  QRect rc = rect();
  painter.drawText(rc, m_strTitle, QTextOption(Qt::AlignCenter));
}

void SDialogTitle::mousePressEvent(QMouseEvent* e) {
  if (!m_pParent || 0 == (m_pParent->windowFlags() & Qt::Window)) {
    QWidget::mousePressEvent(e);
    return;
  }

  if (!m_pParent->isMaximized()) {
    m_bLButtonDown = true;
    m_posMouseDown = e->globalPos();
  }
  QWidget::mousePressEvent(e);
}

void SDialogTitle::mouseReleaseEvent(QMouseEvent* e) {
  m_bLButtonDown = false;
  QWidget::mouseReleaseEvent(e);
}

void SDialogTitle::mouseDoubleClickEvent(QMouseEvent* e) {
  if (e->button() != Qt::LeftButton) {
    QWidget::mouseDoubleClickEvent(e);
    return;
  }

  emit mouseDblClick();
  if (!m_pParent || 0 == (m_pParent->windowFlags() & Qt::Window)) {
    QWidget::mouseDoubleClickEvent(e);
    return;
  }

  if (m_pParent->isMaximized()) {
    m_btnFull->setVisible(true);
    m_btnRestore->setVisible(false);
    m_pParent->showNormal();
  } else {
    m_btnFull->setVisible(false);
    m_btnRestore->setVisible(true);
    m_pParent->showMaximized();
  }

  m_bLButtonDown = false;
  QWidget::mouseDoubleClickEvent(e);
}

void SDialogTitle::mouseMoveEvent(QMouseEvent* e) {
  if (!m_bLButtonDown) {
    QWidget::mouseMoveEvent(e);
    return;
  }

  QPoint offset = e->globalPos() - m_posMouseDown;
  if (e->buttons().testFlag(Qt::LeftButton) && offset.manhattanLength() >= 5) {
    QWidget* parent = qobject_cast<QWidget*>(this->parent());
    if (parent) {
      parent->move(parent->pos() + offset);
      m_posMouseDown = e->globalPos();
    }
  }

  QWidget::mouseMoveEvent(e);
}

void SDialogTitle::onButtonClick() {
  if (!m_pParent) return;
  if (sender() == m_btnPopup) {
    emit popup();
  } else if (sender() == m_btnClose) {
    emit m_pParent->reject();
  } else if (sender() == m_btnFull) {
    m_btnFull->setVisible(false);
    m_btnRestore->setVisible(true);
    m_pParent->showMaximized();
  } else if (sender() == m_btnRestore) {
    m_btnFull->setVisible(true);
    m_btnRestore->setVisible(false);
    m_pParent->showNormal();
  } else if (sender() == m_btnTopping) {
    if (m_pParent->windowFlags() & Qt::WindowStaysOnTopHint) {
      m_btnTopping->setImgId("widgets_win_top");
      m_pParent->setWindowFlag(Qt::WindowStaysOnTopHint, false);
    } else {
      m_btnTopping->setImgId("widgets_win_top_selected");
      m_pParent->setWindowFlag(Qt::WindowStaysOnTopHint);
    }
    m_pParent->show();
  } else if (sender() == m_btnDisplay) {
    m_btnDisplay->setVisible(false);
    m_btnHide->setVisible(true);
    m_pParent->setMinimumHeight(314);
  } else if (sender() == m_btnHide) {
    m_btnHide->setVisible(false);
    m_btnDisplay->setVisible(true);
    m_pParent->setMinimumHeight(29);
  } else if (sender() == m_btnSetting) {
    emit setting();
  }
}
